@extends('layouts.back_stage')
@section('content')
    <style>
        .address {
            border-color: #cfdadd;border-radius: 2px;width: 150px;
            height: 30px;padding: 6px 10px;font-size: 12px;line-height: 1.42857143;margin-right:10px;
            color: #555;background-color: #fff;background-image: none;border: 1px solid #cfdadd;box-shadow: none;
            -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
            -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
        }
        table,th,td{text-align: center}
        b{color:red;}
        .count_sp{padding-bottom:20px;display:inline-block;}
        .add_order{
            float:right;
        }
        .model_box{
            width:100%;
            height:100%;
            background-color:rgba(0,0,0,0.7);
            position: absolute;
            top:0;
            left:0;
            z-index: 99;
            display: none;
        }
        .model{
            width:300px;
            height:200px;
            background-color:white;
            border-radius: 10px;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-150px;
            margin-top:-75px;
        }
        .model p{
            text-align: center;
            margin-top:10px;
            font-size:16px;
        }
        .model select{
            width:200px;
            margin-left:50px;
        }
        .model button{
            margin-top:20px;
        }
        .model-cancel{
            margin-left:50px;
        }
        .model-check0{
            margin-left:90px;
        }
        .select2-container{
            margin-left:50px!important;
        }
        .select2-dropdown{
            margin-left:-50px;
        }
        .pull-left .select2-container{
            margin-left:0px!important;
        }
        .pipe_line{
            margin-top:10px;
            width:200px;
            margin-left:50px;
            height:75px;
            resize:none;
        }
    </style>
    <link rel="stylesheet" href="{{env('CDN_DOMAIN_NAME')}}/css/commoncss/select2.min.css">
    <link href="{{env('CDN_DOMAIN_NAME')}}/store/commoncss/simplePagination.css" rel="stylesheet">
    <div class="container">
        <div style="padding:20px 0;"><h3>补录退货单列表<button class="btn btn-info add_order" onclick="showModel()">添加退货单</button></h3></div>
        <div class="pull-left" style="width: 100%;margin-bottom: 20px">
            <label>选择店铺：</label>
            <select name="" class="address comp" data-placeholder="请选择店铺">
                <option value="">全部店铺</option>
            </select>
            <label>退货状态：</label>
            <select name="" class="address order_status">
                <option value="">全部</option>
                <option value="1">正在退货</option>
                <option value="2">退货完成</option>
            </select>
            <label>商品条码：</label>
            <input type="text" class="form-control qr_code" placeholder="请输入商品条码" style="width:200px;display: inline-block">
            <button onclick="souSan()" class="btn btn-info" style="margin-bottom:4px;">搜索</button>
            <button onclick="reSetScan()" class="btn btn-info" style="margin-bottom:4px;">清空</button>
        </div>
        <span class="count_sp"></span>
        <table class="table table-hover">
            <thead>
            <tr>
                <th>店铺名称</th>
                <th>创建人</th>
                <th>创建时间</th>
                <th>已扫描件数</th>
                <th>退货状态</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
        <div id="paging1" class="page" style=""></div>
        <div class="model_box">
            <div class="model">
                <p>添加退货单</p>
                <select class="js-example-basic-single">
                    <option value="">请选择店铺</option>
                </select>
                <textarea placeholder="请填写备注" class="form-control pipe_line"></textarea>
                <button class="btn btn-info model-check0" style="margin-left:50px;">确定</button>
                <button class="btn btn-info model-cancel" style="margin-left:90px;" onclick="removeModel()">取消</button>

            </div>
        </div>
    </div>
    <script src="{{env('CDN_DOMAIN_NAME')}}/js/commonjs/select2.min.js?v=1.0.2"></script>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/store/commonjs/jquery.simplePagination.js?v0.0.3" type="text/javascript"></script>
    <script>
        var skip = 0;
        /*店铺列表*/
        $(document).ready(function(){
            $('.comp').select2({
                minimumResultsForSearch:-1,
                placeholder: "请选择一个店铺",
                ajax: {
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                    },
                    url: "/picking/get-franchise-lists",
                    dataType: 'json',
                    type:'post',
                    delay: 250,
                    data: function (params) {
                        return {
                            // girard:$('.girard').val()
                        };
                    },
                    processResults: function (data) {
                        $('.js-example-basic-single').select2('data',null);
                        if(data.result.success == true){
                            $.each(data.content,function(idx,obj){
                                var returnText = data.content[idx].name;
                                delete(data.content[idx].name);
                                data.content[idx].text=returnText;
                            });
                            data.content.unshift({'address_details':'','area_code':'000000','area_name':'','shop_type':'','id':'0','text':'全部店铺'});
                            if(data.content[0] == undefined){
                                return {results: data.content};
                            }else{
                                return {results: data.content};
                            }
                        }else{
                            $(".faceImg").attr("src","/img/weep.png");
                            $(".oHandle").text(data.result.errorMsg);
                            $(".oSuccess").show(0).delay(1000).hide(0);
                            return false;
                        }
                    },
                    cache: true
                },
                escapeMarkup: function (markup) { return markup; },
            });
            pervNextSerach();
        });
        /*店铺列表*/
        $('.js-example-basic-single').select2({
            minimumResultsForSearch:-1,
            ajax: {
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                url: "/picking/get-franchise-lists",
                dataType: 'json',
                type:'post',
                delay: 250,
                data: function (params) {
                    return {
                        // girard:$('.girard').val()
                    };
                },
                processResults: function (data) {
                    $('.js-example-basic-single').select2('data',null);
                    if(data.result.success == true){
                        $.each(data.content,function(idx,obj){
                            var returnText = data.content[idx].name;
                            delete(data.content[idx].name);
                            data.content[idx].text=returnText;
                        });
                        if(data.content[0] == undefined){
                            return {
                                results: data.content
                            };
                        }else{
                            return {
                                results: data.content
                            };
                        }
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                        return false;
                    }
                },
                cache: true
            },
            escapeMarkup: function (markup) { return markup; },
        });
        /*搜索*/
        function souSan(){
            skip=0;
            pervNextSerach();
        }
        /*清空*/
        function reSetScan(){
            skip = 0;
            $('.comp').val(null).trigger("change");
            $('.order_status').val("");
            $('.qr_code').val("");
            pervNextSerach();
        }
        /*添加退货单*/
        function showModel(){
            $('.model_box').show();
            $('.js-example-basic-single').select2('data',null);
            $(".pipe_line").val("");
        }
        /*取消*/
        function removeModel(){
            $('.model_box').hide();
        }
        /*确定*/
        $('.model-check0').click(function(){
            var shop_id = $(".js-example-basic-single").select2("val");
            if(shop_id==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text('请选择店铺');
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false
            }
            var pipe_line = $('.pipe_line').val();
            $.ajax({
                url:'/picking/create-return-franchise',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'post',
                dataType:'json',
                data:{
                    shop_id:shop_id,
                    remark:pipe_line
                },
                success:function(res){
                    if(res.result.success == true){
                        $('.table tbody').prepend('<tr>'+
                            '<td>'+res.content.shop_name+'</td>'+
                            '<td>'+res.content.admin_name+'</td>'+
                            '<td>'+res.content.create_time+'</td>'+
                            '<td>'+res.content.return_num+'</td>'+
                            '<td style="color:#7266ba;">正在退货</td>'+
                            '<td>'+res.content.remark+'</td>'+
                            '<td>' +
                            '<a class="btn btn-info" href="/picking/return-franchise-scan/'+res.content.id+'">退货</a>' +
                            '<a class="btn btn-danger delTui" href="javascript:;" abc='+res.content.id+' style="margin-left:10px;">删除</a>' +
                            '</td>'+
                            '</tr>');
                        removeModel();
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(res.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            });
        });
        /*刚进页面加载数据*/
        function pervNextSerach(){
            var shop_id = $('.comp').select2("val");
            var status = $('.order_status').val();
            var small_qr = $('.qr_code').val();
            $.ajax({
                url:'/picking/franchise-lists',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type:'post',
                dataType:'json',
                data:{
                    skip:skip,
                    shop_id:shop_id,
                    status:status,
                    small_qr:small_qr
                },
                success:function(res){
                    //console.log(res);
                    if(res.result.success == true){
                        var trData = '';
                        $.each(res.content.data,function(idx,obj){
                            if(obj.status == 1){
                                trData += '<tr>'+
                                    '<td>'+obj.shop_name+'</td>'+
                                    '<td>'+obj.admin_name+'</td>'+
                                    '<td>'+obj.create_time+'</td>'+
                                    '<td>'+obj.return_nums+'</td>'+
                                    '<td style="color:#7266ba;">正在退货</td>'+
                                    '<td>'+obj.remark+'</td>'+
                                    '<td>' +
                                        '<a class="btn btn-info" href="/picking/return-franchise-scan/'+obj.id+'">退货</a>' +
                                        '<a class="btn btn-danger delTui" href="javascript:;" abc='+obj.id+' style="margin-left:10px;">删除</a>' +
                                    '</td>'+
                                    '</tr>'
                            }else if(obj.status == 2){
                                trData += '<tr>'+
                                    '<td>'+obj.shop_name+'</td>'+
                                    '<td>'+obj.admin_name+'</td>'+
                                    '<td>'+obj.create_time+'</td>'+
                                    '<td>'+obj.return_nums+'</td>'+
                                    '<td style="color:#1c84c6;">退货完成</td>'+
                                    '<td>'+obj.remark+'</td>'+
                                    '<td><a class="btn btn-info" href="/picking/return-franchise-scan/'+obj.id+'">查看</a></td>'+
                                    '</tr>'
                            }
                        });
                        skip = res.content.skip;
                        $('.count_sp').text('共'+res.content.count+'条数据');
                        $("#paging1").pagination({
                            items: res.content.count,
                            itemsOnPage: 10,
                            cssStyle: 'light-theme',
                            currentPage:res.content.skip
                        });
                        $('.table tbody').html(trData);
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(res.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            });
        }
        /*点击分页*/
        $(document).on('click','.page-link',function(){
            if($(this).attr('class') === "page-link next nextPage"){
                if($(this).parent().attr('class') == 'active'){
                    return false;
                }else{
                    pervNextSerach();
                }
            }else if($(this).attr('class') === "page-link prev prevPage"){
                if($(this).parent().attr('class') == 'active'){
                    return false;
                }else{
                    skip=Number(skip)-2;
                    pervNextSerach();
                }
            }else if($(this).attr('class') === "page-link tcdNumber"){
                skip=Number($(this).text())-1;
                pervNextSerach();
            }
        });
        /*删除*/
        $(document).on("click",".delTui",function(){
            var id=$(this).attr("abc");
            $(".oText").text("确认删除该条退货单吗？");
            $(".oMask").show();
            $(".oAnim").show();
            $(".oBtn0").attr("abc",id);
            $(".oBtn0").attr('id','');
            $(".oBtn0").attr('id','sureOTui');
        });
        /*点确定*/
        $(document).on('click','#sureOTui',function(){
            $(".oMask").hide();
            $(".oAnim").hide();
            var id=$(this).attr("abc");
            $.ajax({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                type: 'post',
                url: '/picking/delete-return-franchise',
                data: {
                    id:id,
                },
                success: function (data) {
                    if(data.result.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("删除成功");
                        $(".oSuccess").show(0).delay(800).hide(0);
                        window.location.reload();

                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.result.errorMsg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });
    </script>
@endsection